package cn.diancan.model;

import java.io.Serializable;
import java.util.Collection;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;

import com.opensymphony.xwork2.ActionContext;

/**
 * 管理员
 * @author Sky
 */
@Entity
public class SysManager implements Serializable{

    /**
     * 
     */
    private static final long serialVersionUID = -349200548126830480L;
    private Long id;
    private String userName;
    private String passWd;
    private String phoneNum;//管理员手机号码
    private String role;
    private Set<SysRole> roles;//角色集合
    private Integer isDelete;
    
    
    
    public SysManager() {
	this.isDelete = 0;
    }

	/**
     * 判断是否有该名字的权限
     * @return
     */
    public boolean hasAuthorityByName(String name) {
    	if (hasBestAuthority()) {
			return true;
		}
    	for (SysRole bRole : roles) {
			for (SysAuthority authority : bRole.getYmAuthorities()) {
				if (name.equals(authority.getName())) {				
						return true;
					
				}
			}
		}
		return false;
		
	}
    
    /**
	 * 判断本用户是否有指定URL的权限
	 * 
	 * @param privUrl
	 * @return
	 */
	public boolean hasPrivilegeByUrl(String privUrl) {
		// 超级管理有所有的权限
		if (hasBestAuthority()) {
			return true;
		}

		// >> 去掉后面的参数
		int pos = privUrl.indexOf("?");
		if (pos > -1) {
			privUrl = privUrl.substring(0, pos);
		}

		// 如果本URL不需要控制，则登录用户就可以使用
		Collection<String> allPrivilegeUrls = (Collection<String>) ActionContext.getContext().getApplication().get("allPrivilegeUrls");
		if (!allPrivilegeUrls.contains(privUrl.substring(1, privUrl.length()))) {
			return true;
		} else {
			// 普通用户要判断是否含有这个权限
			for (SysRole role : roles) {
				for (SysAuthority priv : role.getYmAuthorities()) {
					if (privUrl.equals("/"+priv.getUrl())) {
						return true;
					}
				}
			}
			return false;
		}
	}
    
    /**
     * 判断是否是超级管理员
     * @return
     */
    public boolean hasBestAuthority() {
		
		return "root".equals(userName);
	}
    
    @Id
    @GeneratedValue
    public Long getId() {
        return id;
    }
    public void setId(Long id) {
        this.id = id;
    }
    
    public String getUserName() {
        return userName;
    }
    public void setUserName(String userName) {
        this.userName = userName;
    }
    public String getPassWd() {
        return passWd;
    }
    public void setPassWd(String passWd) {
        this.passWd = passWd;
    }
    public String getRole() {
        return role;
    }
    public void setRole(String role) {
        this.role = role;
    }
    public Integer getIsDelete() {
        return isDelete;
    }
    public void setIsDelete(Integer isDelete) {
        this.isDelete = isDelete;
    }
    public String getPhoneNum() {
        return phoneNum;
    }
    public void setPhoneNum(String phoneNum) {
        this.phoneNum = phoneNum;
    }
    
	@ManyToMany(
			targetEntity=SysRole.class,
			cascade={CascadeType.PERSIST,CascadeType.MERGE },
			fetch=FetchType.EAGER
			)
	@JoinTable(
			name="sysmanager_role",
			joinColumns=@JoinColumn(name="managerId"),
			inverseJoinColumns=@JoinColumn(name="roleId")
			)
	public Set<SysRole> getRoles() {
		return roles;
	}
	public void setRoles(Set<SysRole> roles) {
		this.roles = roles;
	}
    
    
    
}